package com.pengli.mytools.ui.mainPage.mine;

import android.content.Intent;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.pengli.mytools.MyApplication;
import com.pengli.mytools.R;
import com.pengli.mytools.beans.po.URLData;
import com.pengli.mytools.retrofit.apiService.BaiduYunTokenService;
import com.pengli.mytools.retrofit.NetConstant;
import com.pengli.mytools.beans.vo.baiduyun.TokenRespVo;
import com.pengli.mytools.ui.base.BaseActivity;
import com.pengli.mytools.ui.webView.MyWebViewActivity;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


/**
 * 配置百度云 获取token
 */
public class BaiduYunConfigActivity extends BaseActivity {

    @BindView(R.id.code)
    EditText code;
    @BindView(R.id.token)
    TextView token;

    @Override
    protected void initView() {
        setContentView(R.layout.activity_get_token);
        ButterKnife.bind(this);

//        Intent intent = new Intent(this, MyWebViewActivity.class);
//        URLData urlData = new URLData();
//        urlData.setUrl(NetConstant.authorizeUrl);
//        intent.putExtra("urldata", urlData);
//        startActivity(intent);
    }

    @Override
    protected void initData() {



    }

    @Override
    protected void setListener() {

    }

    @OnClick(R.id.start)
    public void onClick(View view){
        HashMap<String, String> parm = new HashMap<>();
        parm.put("grant_type","authorization_code");
        parm.put("code",code.getText().toString());
        parm.put("client_id", NetConstant.AppKey);
        parm.put("client_secret", NetConstant.SecretKey);
        parm.put("redirect_uri", "oob");

        BaiduYunTokenService baiduYunTokenService = MyApplication.getInstance().getBaiduYunTokenService();
        Call<TokenRespVo> accessToken = baiduYunTokenService.getAccessToken(parm);
        accessToken.enqueue(new Callback<TokenRespVo>() {
            @Override
            public void onResponse(Call<TokenRespVo> call, Response<TokenRespVo> response) {

                TokenRespVo body = response.body();
                if (body!=null) {
                    String access_token = body.getAccess_token();
                    token.setText(access_token);

                    MyApplication.getInstance().setAccess_token(access_token);
                }
            }

            @Override
            public void onFailure(Call<TokenRespVo> call, Throwable t) {

            }
        });

    }
}
